/**
 * Created by zhanglihe on 15/7/8.
 */

require.config(require_config);

define([
    'jquery',
    'common'
], function( $, common ) {

    console.log('login');
    var main = function() {
        this.init();
    };

    main.prototype = {
        init: function() {
            this.login_bg();
            this.event();
        },
        login_bg: function() {
            var n = Math.floor(Math.random()*15+1);
            $('#bg').addClass('bg' + n);
        },
        event: function() {
            var _this = this;
            $('#btn-login').on('click', function(e) {
                e.preventDefault();
                var username = $('input[name="username"]').val();
                var password = $('input[name="password"]').val();
                var module   = $('input[name="module"]').val();
                _this.login({username: username, password: password, module: module});
            });
            $('input').focus(function() {
                $('#bg').addClass('animate-blur');
            });
        },
        login: function(data) {
            $.ajax({
                url: '/auth',
                type: 'post',
                data: data,
                async: false,
                dataType: 'JSON',
                success: function(res) {
                    if(res.code == 0 && res.redirect_url != '') {
                        window.location.href = res.redirect_url;
                    } else {
                        Alert({msg: res.msg, type: 'error'});
                    }
                }
            });
        }
    };

    (new main());

});
